#include "entityobject.h"

using namespace Opl::Core;
QString EntityObject::ID = "Object";

QScriptValue EntityObject::toScriptValue(QScriptEngine *engine, EntityObject* const &in)
{
    return engine->newQObject(in);
}

void EntityObject::fromScriptValue(const QScriptValue &object, EntityObject* &out)
{
    out = qobject_cast<EntityObject*>(object.toQObject());
}

QScriptValue EntityObject::construct_Script(QScriptContext *context, QScriptEngine *engine)
{
	return engine->newQObject(new EntityObject(0));
}

EntityObject::EntityObject(EntityObject *parent) :
	QObject(parent), Entity()
{
}

EntityObject::~EntityObject()
{

}

QDeclarativeListProperty<QObject> EntityObject::childerns()
{
    return QDeclarativeListProperty<QObject>(this, 0, EntityObject::child_append);
}

void EntityObject::child_append(QDeclarativeListProperty<QObject> *prop, QObject *o)
{
    if (!o)
        return;

    QDeclarativeItem *that = static_cast<QDeclarativeItem *>(prop->object);

    // This test is measurably (albeit only slightly) faster than qobject_cast<>()
    const QMetaObject *mo = o->metaObject();
    while (mo && mo != &QGraphicsObject::staticMetaObject) mo = mo->d.superdata;

    o->setParent(that);

    if (mo) {
       // static_cast<QGraphicsObject *>(o)->setParentItem(that);
        //QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(o))->setParentItemHelper(that, 0, 0);
    } else {

    }
}

void EntityObject::notify(const Notify& n)
{
	logger()->trace("Notify from:%1", toString());
}


QString EntityObject::toString()
{
	return QString("[%1(id:%2 stamp:%3 tag:%4)]").arg(metaObject()->className()).arg(unique().toString()).arg(stamp().toMSecsSinceEpoch()).arg(tag().typeName());
}
